package com.usian.controller;

import com.usian.pojo.User;
import com.usian.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Date;
import java.util.List;


@Controller
@RequestMapping("/user")
public class UserController {

    private static final Logger logger= LoggerFactory.getLogger(UserController.class);

    @Autowired
    private UserService userService;

   @RequestMapping("/show")
    public String show(Model model) {
        model.addAttribute("name","张三");
        model.addAttribute("date",new Date());
        model.addAttribute("msg","王轲轲真可爱,头上长个狗脑袋");
        logger.debug("王轲轲真可爱,头上长个狗脑袋");
        return "index";
    }


    @RequestMapping("/findAll")
    public String findAll(Model model){
        List<User> list = userService.findAll();
        model.addAttribute("list",list);
        return "show";
    }

    @RequestMapping("/toAdd")
    public String toAdd(){
            return "add";
    }

    @RequestMapping("/add")
    public String add(User user){
        if (user!=null && user.getUserId()!=null){
            userService.update(user);
            return "redirect:/user/findAll";
        }else{
            userService.add(user);
            return "redirect:/user/findAll";
        }
    }

    @RequestMapping("/findById")
    public String  findById(Integer userId,Model model){
        User u = userService.findById(userId);
        model.addAttribute("u",u);
        return "update";
    }

    @RequestMapping("/delete")
    public String delete(Integer userId){
        userService.delete(userId);
        return "redirect:/user/findAll";
    }


}
